<template>
  <div>
    <table cellspacing="0" class="table">
      <thead>
        <tr>
          <th width="50"><Checkbox v-model="selectAll" @on-change="onSelectAll"></Checkbox></th>
          <th width="20"></th>
          <th v-for="item in listData" :key="item.id" :width="item.width">{{item.title}}</th>
        </tr>
      </thead>
      <tbody v-for="(item, index) in viewData" :key="index">
        <tr>
          <td><Checkbox v-model="item.checked" @on-change="onJoinSaleCheck(item)"></Checkbox></td>
          <td class="cursor-title">
            <Icon type="ios-arrow-down" size="20" v-if="item.showChildren" @click="onShowChild(item)"></Icon>
            <Icon type="ios-arrow-forward" size="20" v-if="item.productSkuList && item.productSkuList.length && !item.showChildren" @click="onShowChild(item)"></Icon>
          </td>
          <td>{{item.code}}</td>
          <td>-</td>
          <td class="cursor-title" :title="item.title"><div class="e1 flex-start"><img :src="item.productPrimaryImg+'_90w'" alt="" >{{item.title}}</div></td>
          <td>{{getMinMaxValue(item.productSkuList, {index: 'salePrice'})}}</td>
          <td>{{item.isShelf | status}}</td>
          <td>{{getSum(item.productSkuList, {index: 'stock'})}}</td>
          <td>{{getMinMaxValue(item.productSkuList, {index: 'returnPrice'})}}</td>
          <td>{{getMinMaxValue(item.productSkuList, {index: 'quantity'})}}</td>
          <td>
              <Button  size="small" @click="onDeleteSale(index,list)">删除</Button>
          </td>
        </tr>
        <tr v-if="item.productSkuList && item.productSkuList.length"  v-for="(child, index1) in item.productSkuList" :key="index1" v-show="item.showChildren">
          <td></td>
          <td></td>
          <td><Checkbox v-model="child.checked" @on-change="onJoinSaleCheck(child,item)"></Checkbox></td>
          <td>{{child.barcode}}</td>
          <td class="cursor-title" :title="item.title"><div class="e1 flex-start"><img :src="item.productPrimaryImg+'_90w'" alt="">{{item.title}}</div></td>
          <td>{{child.salePrice}}</td>
          <td>{{item.isShelf  | status}}</td>
          <td>{{child.stock}}</td>
          <td width="80"><InputNumber v-model="child.returnPrice"></InputNumber></td>
          <td width="80"><InputNumber :min="1" :max="20" v-model="child.quantity"></InputNumber></td>
          <td>
              <Button  @click="onDeleteSale(index1, item.productSkuList,index,list)" size="small">删除</Button>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="pager">
      <Page 
        :total="totalItem" 
        :current="currentPage"
        :page-size="pageSize"
        @on-change="onPageChange"
        @on-page-size-change="onPageSizeChange"
        show-sizer 
        placement="top">
      </Page>
    </div> 
    <Loader :status="status" @refresh="getData"></Loader>
  </div>
</template>
<script>
import {
  getMinValue,
  getMaxValue,
  getSum
} from "@/views/sales/common/TableSaleMethods";
export default {
  props: {
    listData: '',
    list:'',
    status: ''
  },
  data() {
    return {
      selectAll: false,
      totalItem: 1,
      pageSize:  window.$pageSize,
      currentPage: 1,
      viewData: []
    }
  },
  created () {
    this.initData()
  },
  methods: {
    initData () {
      this.totalItem = this.list.length;
      this.viewData =  this.list.slice(0, this.pageSize);
    },
    getSum(param, object) {
      return getSum(param, object)
    },
    getMinMaxValue(param, object) {
      if(getMinValue(param, object)==getMaxValue(param, object)) {
        return `[${getMinValue(param, object)}]`
      }else {
        return  `[${getMinValue(param, object)}-${getMaxValue(param, object)}]`;
      }
    },
    onSelectAll() {
      // 全选 、全不选
      this.$emit('onChangeSelectAll',this.selectAll);
    },
    onJoinSale (child, item) {
      this.$emit('onJoinSale',child, item);
    },
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    onJoinSaleCheck (child,item) {
      this.$emit('onJoinSaleCheck', child,item)
    },
    onDeleteSale (index, data, index1, data1) {
      this.$emit('onDeleteSale', index, data, index1, data1)
    },
    getData() {},
    onPageChange(val) {
      this.viewData =  this.list.slice(((val-1) * this.pageSize),val * this.pageSize);
    },
    onPageSizeChange(val) {
      this.pageSize= val;
      this.viewData = this.list.slice(0,val)
    }
  },
  filters: {
    status (val) {
      if (val) {
        return '上架'
      }else {
        return '下架'
      }
    }
  },
  watch: {
    list () {
      this.initData()
    }
  }
}
</script>

